/*************************************************
 * File:    		SMXMLDOM.cpp
 * Author:  	HouJiong <hou_jiong@163.com>
 * Brief:   		StoneMoneky project.
 *
 * Copyright (c) HouJiong
 *
 *************************************************/

/*
 * History:
 * =======================================
 * 2009-04-10 HouJiong <hou_jiong@163.com> created
 */
 
#include "SMXMLDOM.h"
#include "SMtypedef.h"



Element::Element()
{
	m_tag="NULL";		
	m_y="NULL";
	m_x="NULL";
	m_width="NULL";
	m_height="NULL";
	m_id="NULL";
	m_text="NULL";
	m_src="NULL";
	m_onClicked="NULL";
}

std::string Element::getAttribute(std::string attribute)
{
		if(attribute=="tag")
		{
			return m_tag;
		}
		else if(attribute=="x")
		{
			return m_x;
		}
		else if(attribute=="y")
		{
			return m_y;
		}
		else if(attribute=="width")
		{
			return m_width;
		}
		else if(attribute=="height")
		{
			return m_height;
		}
		else if(attribute=="id")
		{
			return m_id;
		}
		else if(attribute=="text")
		{
			return m_text;
		}
		else if(attribute=="src")
		{
			 return m_src;
		}
		else if(attribute=="onClicked")
		{
			 return m_onClicked;
		}
		else
		{
			return "NULL";
		}
				
}

void Element::setAttribute(std::string attribute, std::string value)
{
		if(attribute=="tag")
		{
			m_tag=value;
		}
		else if(attribute=="x")
		{
			 m_x=value;
		}
		else if(attribute=="y")
		{
			 m_y=value;
		}
		else if(attribute=="width")
		{
			 m_width=value;
		}
		else if(attribute=="height")
		{
			 m_height=value;
		}
		else if(attribute=="id")
		{
			 m_id=value;
		}
		else if(attribute=="text")
		{
			 m_text=value;
		}
		else if(attribute=="src")
		{
			 m_src=value;
		}
		else if(attribute=="onClicked")
		{
			 m_onClicked=value;
		}
				
}
	
SMXMLDOM::SMXMLDOM()
{
	m_xmldoc = NULL;  
	m_xmlrootnode = NULL;  
}

bool SMXMLDOM::loadXML(std::string xmlfile)
{
	DEBUG_LINE();
	m_xmldoc=xmlReadFile(xmlfile.c_str(), NULL, 0);
   	if (m_xmldoc == NULL)
	{  
		DEBUG_ERROR();
		return false;
   	}    

	m_xmlrootnode= xmlDocGetRootElement(m_xmldoc); 
	if(m_xmlrootnode == NULL)
	{
		DEBUG_ERROR();
		return false;
	}

	if((ConvertToString(m_xmlrootnode->name))!="view")
	{
		DEBUG_ERROR();
		return false;
	}
	
	Element newElement;
	newElement.setAttribute( "tag", "view");
	newElement.setAttribute( "x", xmlPropToString(m_xmlrootnode,(xmlChar*)"x"));
	newElement.setAttribute( "y",xmlPropToString(m_xmlrootnode,(xmlChar*)"y"));
	newElement.setAttribute( "width", xmlPropToString(m_xmlrootnode,(xmlChar*)"width"));
	newElement.setAttribute( "height",xmlPropToString(m_xmlrootnode,(xmlChar*)"height"));
	newElement.setAttribute( "src",xmlPropToString(m_xmlrootnode,(xmlChar*)"src"));
	
	m_ElementList.push_back(newElement);
	return parse(m_xmlrootnode);
}

bool SMXMLDOM::parse(xmlNode *parentXMLNode)
{
 
	for (xmlNode *element=parentXMLNode->xmlChildrenNode; element; element = element->next)
	{      
		
		if(element->type == XML_ELEMENT_NODE)	
		{
			
			Element newElement;

			newElement.setAttribute( "tag",ConvertToString(element->name));
			newElement.setAttribute( "x", xmlPropToString(element,(xmlChar*)"x"));
			newElement.setAttribute( "y",xmlPropToString(element,(xmlChar*)"y"));
			newElement.setAttribute( "width", xmlPropToString(element,(xmlChar*)"width"));
			newElement.setAttribute( "height",xmlPropToString(element,(xmlChar*)"height"));

			newElement.setAttribute( "id",xmlPropToString(element,(xmlChar*)"id"));
			newElement.setAttribute( "text",xmlPropToString(element,(xmlChar*)"text"));
			newElement.setAttribute( "src",xmlPropToString(element,(xmlChar*)"src"));
			newElement.setAttribute( "onClicked",xmlPropToString(element,(xmlChar*)"onClicked"));
			
			m_ElementList.push_back(newElement);
		//	parse(element);
						
		}
	}

	return true;
}



